package day6.Component;

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

public class TestTimer extends JFrame {
	public TestTimer(String title) {
		this.setTitle(title);
		this.setSize(400, 500);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		initComponent();
		
		// 放到最后显示窗口
		this.setVisible(true);
	}
	// 初始化
	private void initComponent() {
		this.setLayout(new FlowLayout());
		JLabel label = new JLabel("10");
	     label.setText("10");
	     label.setFont(new Font("宋体",0,50));
		
		//actionlistener 动作监听器
		Timer timer = new Timer(1000,new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Timer timer = (Timer) e.getSource();
				//System.out.println(e.getSource().getClass());
                //      System.out.println("Hello world!");				
			     //拿到标签数字-1重新放到标签
                   String textStr = label.getText();
                   //字符串转数字
                   int textInt = Integer.parseInt(textStr);
                   //再减一
                   textInt--;
                   label.setText(textInt+"");
                   if(textInt==0) {
                	   timer.stop();
                	   
                   }
			}
			
		});
		timer.start();
		this.add(label);
	}
	public static void main(String[] args) {
		new TestTimer("");
	}


}
